package org.wyf.EveryPoint.common.filter;


import lombok.extern.slf4j.Slf4j;
import org.wyf.EveryPoint.system.service.IConfigService;
import org.wyf.EveryPoint.system.service.IDeptService;

import java.io.IOException;
import java.net.InetAddress;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
/**
 * 品牌商域名拦截器
 *
 * @author lw
 */
@Slf4j
public class DomainFilter implements Filter {
	
	private IDeptService deptService;
	private IConfigService configService;
	
	public DomainFilter(IDeptService deptService, IConfigService configService) {
		this.deptService = deptService;
		this.configService = configService;
	}

    @Override
    public void init(FilterConfig filterConfig) {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
//        HttpServletRequest req = (HttpServletRequest) request;
//        ParameterRequestWrapper wrapper = new ParameterRequestWrapper(req) ;
//        if(ObjKit.isEmpty(req.getAttribute("publicAgentId"))){
//	    	String serverName = req.getServerName();
//	        log.error("域名：" + serverName);
//	        InetAddress inetAddress = InetAddress.getByName(serverName);
//	        String DEFAULT_DEPT_ID = configService.getValueByKey(FebsConstant.DEFAULT_DEPT_ID);
//	        if(StrKit.isEmpty(DEFAULT_DEPT_ID)) {
//	    	    DEFAULT_DEPT_ID = "2";
//	        }
//	        Long agentId = Long.valueOf(DEFAULT_DEPT_ID);
//	        if(!serverName.equals("localhost") && !serverName.equals("127.0.0.1") && !serverName.equals(inetAddress.getHostAddress())) {
//	        	String name = serverName.substring(0, serverName.indexOf("."));
//	        	log.error("二级域名：" + name);
//	        	Long id = deptService.getDeptIdByDomain(name);
//	        	log.error("结盟商Id：" + id);
//	        	if(null != id) {
//	        		agentId = id;
//	        	}
//	        }
//            wrapper.addParameter("publicAgentId", agentId);
//            chain.doFilter(wrapper, response);
//	        return;
//        }
//        chain.doFilter(req, response);
    }


    @Override
    public void destroy() {
        // do nothing
    }

}
